﻿#define Fun1
#define Clear

using ConsoleTool.Models.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Xml;

namespace ConsoleTool
{
    class Program
    {
        static void Main(string[] args)
        {
            //NewClassLib();

            ReplaceAspNetCoreApp();

            Console.ReadKey();
        }


        static void ReplaceAspNetCoreApp()
        {
            using (PowerShell ps = PowerShell.Create(RunspaceMode.NewRunspace))
            {
                ps.InvokeScript($@"cd E:\Gitee\ASPNETCORESOURCECODE\AuthenticationLearning");
                string[] packageReference = {"Microsoft.AspNet.WebApi.Client"
,"Microsoft.AspNetCore"
,"Microsoft.AspNetCore.Antiforgery"
,"Microsoft.AspNetCore.Authentication"
,"Microsoft.AspNetCore.Authentication.Abstractions"
,"Microsoft.AspNetCore.Authentication.Cookies"
,"Microsoft.AspNetCore.Authentication.Core"
,"Microsoft.AspNetCore.Authentication.Facebook"
,"Microsoft.AspNetCore.Authentication.Google"
,"Microsoft.AspNetCore.Authentication.JwtBearer"
,"Microsoft.AspNetCore.Authentication.MicrosoftAccount"
,"Microsoft.AspNetCore.Authentication.OAuth"
,"Microsoft.AspNetCore.Authentication.OpenIdConnect"
,"Microsoft.AspNetCore.Authentication.Twitter"
,"Microsoft.AspNetCore.Authentication.WsFederation"
,"Microsoft.AspNetCore.Authorization"
,"Microsoft.AspNetCore.Authorization.Policy"
,"Microsoft.AspNetCore.Connections.Abstractions"
,"Microsoft.AspNetCore.CookiePolicy"
,"Microsoft.AspNetCore.Cors"
,"Microsoft.AspNetCore.Cryptography.Internal"
,"Microsoft.AspNetCore.Cryptography.KeyDerivation"
,"Microsoft.AspNetCore.DataProtection"
,"Microsoft.AspNetCore.DataProtection.Abstractions"
,"Microsoft.AspNetCore.DataProtection.Extensions"
,"Microsoft.AspNetCore.Diagnostics"
,"Microsoft.AspNetCore.Diagnostics.Abstractions"
,"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore"
,"Microsoft.AspNetCore.Diagnostics.HealthChecks"
,"Microsoft.AspNetCore.HostFiltering"
,"Microsoft.AspNetCore.Hosting"
,"Microsoft.AspNetCore.Hosting.Abstractions"
,"Microsoft.AspNetCore.Hosting.Server.Abstractions"
,"Microsoft.AspNetCore.Html.Abstractions"
,"Microsoft.AspNetCore.Http"
,"Microsoft.AspNetCore.Http.Abstractions"
,"Microsoft.AspNetCore.Http.Connections"
,"Microsoft.AspNetCore.Http.Connections.Common"
,"Microsoft.AspNetCore.Http.Extensions"
,"Microsoft.AspNetCore.Http.Features"
,"Microsoft.AspNetCore.HttpOverrides"
,"Microsoft.AspNetCore.HttpsPolicy"
,"Microsoft.AspNetCore.Identity"
,"Microsoft.AspNetCore.Identity.EntityFrameworkCore"
,"Microsoft.AspNetCore.Identity.UI"
,"Microsoft.AspNetCore.JsonPatch"
,"Microsoft.AspNetCore.Localization"
,"Microsoft.AspNetCore.Localization.Routing"
,"Microsoft.AspNetCore.MiddlewareAnalysis"
,"Microsoft.AspNetCore.Mvc"
,"Microsoft.AspNetCore.Mvc.Abstractions"
,"Microsoft.AspNetCore.Mvc.Analyzers"
,"Microsoft.AspNetCore.Mvc.ApiExplorer"
,"Microsoft.AspNetCore.Mvc.Core"
,"Microsoft.AspNetCore.Mvc.Cors"
,"Microsoft.AspNetCore.Mvc.DataAnnotations"
,"Microsoft.AspNetCore.Mvc.Formatters.Json"
,"Microsoft.AspNetCore.Mvc.Formatters.Xml"
,"Microsoft.AspNetCore.Mvc.Localization"
,"Microsoft.AspNetCore.Mvc.Razor"
,"Microsoft.AspNetCore.Mvc.Razor.Extensions"
,"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation"
,"Microsoft.AspNetCore.Mvc.RazorPages"
,"Microsoft.AspNetCore.Mvc.TagHelpers"
,"Microsoft.AspNetCore.Mvc.ViewFeatures"
,"Microsoft.AspNetCore.NodeServices"
,"Microsoft.AspNetCore.Owin"
,"Microsoft.AspNetCore.Razor"
,"Microsoft.AspNetCore.Razor.Design"
,"Microsoft.AspNetCore.Razor.Language"
,"Microsoft.AspNetCore.Razor.Runtime"
,"Microsoft.AspNetCore.ResponseCaching"
,"Microsoft.AspNetCore.ResponseCaching.Abstractions"
,"Microsoft.AspNetCore.ResponseCompression"
,"Microsoft.AspNetCore.Rewrite"
,"Microsoft.AspNetCore.Routing"
,"Microsoft.AspNetCore.Routing.Abstractions"
,"Microsoft.AspNetCore.Server.HttpSys"
,"Microsoft.AspNetCore.Server.IIS"
,"Microsoft.AspNetCore.Server.IISIntegration"
,"Microsoft.AspNetCore.Server.Kestrel"
,"Microsoft.AspNetCore.Server.Kestrel.Core"
,"Microsoft.AspNetCore.Server.Kestrel.Https"
,"Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions"
,"Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets"
,"Microsoft.AspNetCore.Session"
,"Microsoft.AspNetCore.SignalR"
,"Microsoft.AspNetCore.SignalR.Common"
,"Microsoft.AspNetCore.SignalR.Core"
,"Microsoft.AspNetCore.SignalR.Protocols.Json"
,"Microsoft.AspNetCore.SpaServices"
,"Microsoft.AspNetCore.SpaServices.Extensions"
,"Microsoft.AspNetCore.StaticFiles"
,"Microsoft.AspNetCore.WebSockets"
,"Microsoft.AspNetCore.WebUtilities"
,"Microsoft.CodeAnalysis.Razor"
,"Microsoft.EntityFrameworkCore"
,"Microsoft.EntityFrameworkCore.Abstractions"
,"Microsoft.EntityFrameworkCore.Analyzers"
,"Microsoft.EntityFrameworkCore.Design"
,"Microsoft.EntityFrameworkCore.InMemory"
,"Microsoft.EntityFrameworkCore.Relational"
,"Microsoft.EntityFrameworkCore.SqlServer"
,"Microsoft.EntityFrameworkCore.Tools"
,"Microsoft.Extensions.Caching.Abstractions"
,"Microsoft.Extensions.Caching.Memory"
,"Microsoft.Extensions.Caching.SqlServer"
,"Microsoft.Extensions.Configuration"
,"Microsoft.Extensions.Configuration.Abstractions"
,"Microsoft.Extensions.Configuration.Binder"
,"Microsoft.Extensions.Configuration.CommandLine"
,"Microsoft.Extensions.Configuration.EnvironmentVariables"
,"Microsoft.Extensions.Configuration.FileExtensions"
,"Microsoft.Extensions.Configuration.Ini"
,"Microsoft.Extensions.Configuration.Json"
,"Microsoft.Extensions.Configuration.KeyPerFile"
,"Microsoft.Extensions.Configuration.UserSecrets"
,"Microsoft.Extensions.Configuration.Xml"
,"Microsoft.Extensions.DependencyInjection"
,"Microsoft.Extensions.DependencyInjection.Abstractions"
,"Microsoft.Extensions.DiagnosticAdapter"
,"Microsoft.Extensions.Diagnostics.HealthChecks"
,"Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions"
,"Microsoft.Extensions.FileProviders.Abstractions"
,"Microsoft.Extensions.FileProviders.Composite"
,"Microsoft.Extensions.FileProviders.Embedded"
,"Microsoft.Extensions.FileProviders.Physical"
,"Microsoft.Extensions.FileSystemGlobbing"
,"Microsoft.Extensions.Hosting"
,"Microsoft.Extensions.Hosting.Abstractions"
,"Microsoft.Extensions.Http"
,"Microsoft.Extensions.Identity.Core"
,"Microsoft.Extensions.Identity.Stores"
,"Microsoft.Extensions.Localization"
,"Microsoft.Extensions.Localization.Abstractions"
,"Microsoft.Extensions.Logging"
,"Microsoft.Extensions.Logging.Abstractions"
,"Microsoft.Extensions.Logging.Configuration"
,"Microsoft.Extensions.Logging.Console"
,"Microsoft.Extensions.Logging.Debug"
,"Microsoft.Extensions.Logging.EventSource"
,"Microsoft.Extensions.Logging.TraceSource"
,"Microsoft.Extensions.ObjectPool"
,"Microsoft.Extensions.Options"
,"Microsoft.Extensions.Options.ConfigurationExtensions"
,"Microsoft.Extensions.Options.DataAnnotations"
,"Microsoft.Extensions.Primitives"
,"Microsoft.Extensions.WebEncoders"
,"Microsoft.Net.Http.Headers"
,"System.IO.Pipelines"};
                foreach (string package in packageReference)
                {
                    //安装 package
                    ps.InvokeScript($"dotnet add package {package}");
                }
            }
                
        }




        static void NewClassLib()
        {
#if !Fun
            string srcPath = @"E:\GitHub\ASPNETCORE\Security-release-2.2\src",
                securityPath= @"E:\Gitee\ASPNETCORESOURCECODE\Security";
#else
            string srcPath = @"F:\GitHub\Security-release-2.2\src",
                securityPath= @"F:\Gitee\ASPNETCORESOURCECODE\Security";
#endif
            DirectoryInfo srcDirectoryInfo =new DirectoryInfo(srcPath);
            DirectoryInfo[] classLibDirectoryInfos = srcDirectoryInfo.GetDirectories();
            using (PowerShell ps = PowerShell.Create(RunspaceMode.NewRunspace))
            {
                foreach (DirectoryInfo classLibDirectoryInfo in classLibDirectoryInfos)
                {
                    string classLibDirectoryName = $@"{securityPath}\{classLibDirectoryInfo.Name}",
                        srcDirectoryName= $@"{srcPath}\{classLibDirectoryInfo.Name}";
                    ps.InvokeScript($@"cd {securityPath}");

#if Clear
                    if (Directory.Exists(classLibDirectoryName))
                        Directory.Delete(classLibDirectoryName, true);
#endif
                    string srcCsprojFile =
                        $@"{srcDirectoryName}\{classLibDirectoryInfo.Name}.csproj";

                    ps.InvokeScript($"dotnet new classlib -lang \"C#\" -n {classLibDirectoryInfo.Name} -f netcoreapp2.2 --no-restore");
                    File.Delete($@"{classLibDirectoryName}\Class1.cs");
                    List<PackageReference> packageReferences = GetPackageReference(srcCsprojFile);
                    ps.InvokeScript($"cd {classLibDirectoryInfo.Name}");
                    foreach (PackageReference packageReference in packageReferences)
                    {
                        //安装 package
                        ps.InvokeScript($"dotnet add package {packageReference.Include}");
                    }

                    //复制文件
                    DirectoryInfo srcDirectoryInifo = new DirectoryInfo(srcDirectoryName);

                    

                }

            }
        }


        static List<PackageReference> GetPackageReference(string fileName)
        {
            XmlDocument document=new XmlDocument();
            document.Load(fileName);

            XmlNodeList packages = document.SelectNodes("//ItemGroup/PackageReference");
            List<PackageReference> packageReferences=new List<PackageReference>();
            for (var i = 0; i < packages.Count; i++)
            {
                XmlNode package = packages[i];
                if(string.IsNullOrEmpty(package.Attributes["Include"]?.Value)
                   || package.Attributes["Include"].Value.StartsWith(".."))
                    continue;

                packageReferences.Add(new PackageReference()
                {
                    Include = package.Attributes["Include"]?.Value,
                    PrivateAssets = package.Attributes["PrivateAssets"]?.Value,
                    Version = package.Attributes["Version"]?.Value
                });

                Console.WriteLine($"package:{package.Attributes["Include"]?.Value}");
            }

            return packageReferences;
        }
    }
}
